package com.haipumi.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.haipumi.beans.haipumi_exchangelist;
import com.haipumi.dao.DBHelper;
import com.haipumi.dao.IBackExchangeDao;

public class BackExchangeDaoImpl implements IBackExchangeDao {

	DBHelper db=new DBHelper();
	@Override
	public List<haipumi_exchangelist> findallexchangelist() {
		String sql="select * from haipumi_exchangelist ";
		List<haipumi_exchangelist>exchangelist=null;
		try {
			exchangelist=db.finds(sql, null, haipumi_exchangelist.class);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return exchangelist;
	}

	@Override
	public int countAllexchangelist() {
		int r=0;
		String sql="select count(*) from haipumi_exchangelist ";
		try {
			r=(int) db.findDouble(sql, null);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return r;
	}

	@Override
	public haipumi_exchangelist updatExchangeList(
			haipumi_exchangelist exchangelist) {
		String sql=" update haipumi_exchangelist set elidentity=? where elid=?";
		List<Object>params=new ArrayList<Object>();
		params.add(exchangelist.getElidentity());
		params.add(exchangelist.getElid());
		int result=0;
		try {
			result=db.doUpdate(sql, params);
			if(result>0){
				//成功了 
				return exchangelist;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return findExchangeList(exchangelist.getElid());
	}
	
	public haipumi_exchangelist findExchangeList(String elid){
		String sql=" select * from haipumi_exchangelist where elid=? ";
		List<Object>params=new ArrayList<Object>();
		params.add(elid);
		haipumi_exchangelist backexchangelist=null;
		List<haipumi_exchangelist>backexchangelists=null;
		try {
			backexchangelists=db.finds(sql, params, haipumi_exchangelist.class);
			if(!backexchangelists.isEmpty()){
				backexchangelist=backexchangelists.get(0);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return backexchangelist;
		
	}

}
